package com.ruoyi.web.controller.training;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.pdf.PdfExporter;
import com.ruoyi.training.domain.ExamRecord;
import com.ruoyi.training.service.IExamRecordService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 培训记录Controller
 *
 * @author wng
 * @date 2024-08-04
 */
@RestController
@RequestMapping("/training/examRecord")
public class ExamRecordController extends BaseController {
    @Resource
    private IExamRecordService examRecordService;
    
    /**
     * 查询培训记录列表
     */
    @PreAuthorize("@ss.hasPermi('training:examRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(ExamRecord examRecord) {
        startPage();
        LoginUser loginUser = getLoginUser();
        if (loginUser.getUserId() != 1 && examRecord.getDeptId() == null) {
            examRecord.setDeptId(loginUser.getDeptId());
        }
        List<ExamRecord> list = examRecordService.selectExamRecordList(examRecord);
        return getDataTable(list);
    }

    /**
     * 导出培训记录列表
     */
    @PreAuthorize("@ss.hasPermi('training:examRecord:export')")
    @Log(title = "培训记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExamRecord examRecord) throws Exception{
        List<ExamRecord> list = examRecordService.selectExamRecordList(examRecord);
//        ExcelUtil<ExamRecord> util = new ExcelUtil<ExamRecord>(ExamRecord.class);
//        util.exportExcel(response, list, "培训记录数据");
        PdfExporter<ExamRecord> util = new PdfExporter<>();
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "attachment; filename=export.pdf");
        util.exportListToPdf(list,response.getOutputStream());
    }

    /**
     * 获取培训记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('training:examRecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(examRecordService.selectExamRecordById(id));
    }

    /**
     * 新增培训记录
     */
    @PreAuthorize("@ss.hasPermi('training:examRecord:add')")
    @Log(title = "培训记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExamRecord examRecord) {
        LoginUser loginUser = getLoginUser();
        examRecord.setCreateBy(String.valueOf(loginUser.getUserId()));
        examRecord.setDeptId(loginUser.getDeptId());
        return toAjax(examRecordService.insertExamRecord(examRecord));
    }

    /**
     * 报名
     */
    @PreAuthorize("@ss.hasPermi('training:examRecord:userAdd')")
    @Log(title = "报名", businessType = BusinessType.INSERT)
    @PostMapping("/signIn")
    public AjaxResult add_user(@RequestBody ExamRecord examRecord) {
        LoginUser loginUser = getLoginUser();
        examRecord.setUserId(loginUser.getUserId());
        examRecord.setDeptId(loginUser.getDeptId());
        return toAjax(examRecordService.insertExamRecord_user(examRecord));
    }

    /**
     * 修改培训记录
     */
    @PreAuthorize("@ss.hasPermi('training:examRecord:edit')")
    @Log(title = "培训记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExamRecord examRecord) {
        LoginUser loginUser = getLoginUser();
        examRecord.setUpdateBy(String.valueOf(loginUser.getUserId()));
        return toAjax(examRecordService.updateExamRecord(examRecord));
    }

    /**
     * 删除培训记录
     */
    @PreAuthorize("@ss.hasPermi('training:examRecord:remove')")
    @Log(title = "培训记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(examRecordService.deleteExamRecordByIds(ids));
    }
}
